<?php
class ModelBlogSetup extends Model {
	public function createTables() {

		/* Creates Required Tables */ 
		
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_categories` (
							  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
							  `image` varchar(255) NOT NULL DEFAULT '0',
							  `parent_id` smallint(5) unsigned NOT NULL DEFAULT '0',
							  `published` tinyint(1) unsigned NOT NULL DEFAULT '0',
							  `created` datetime NOT NULL,
							  `modified` datetime NOT NULL,
							  `created_by` smallint(5) unsigned NOT NULL DEFAULT '0',
							  `modified_by` smallint(5) unsigned NOT NULL DEFAULT '0',
							  `sort_order` tinyint(3) NOT NULL,
							  `post_count` int(10) unsigned NOT NULL DEFAULT '0',
							  PRIMARY KEY (`id`)
							) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_categories_description` (
							  `blog_category_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `language_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
							  `description` text COLLATE utf8_bin NOT NULL,
							  `meta_description` varchar(255) COLLATE utf8_bin NOT NULL,
							  `meta_keyword` varchar(255) COLLATE utf8_bin NOT NULL,
							  PRIMARY KEY (`blog_category_id`,`language_id`),
							  KEY `name` (`name`)
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_categories_to_layout` (
							  `blog_category_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `store_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `layout_id` int(10) unsigned NOT NULL DEFAULT '0',
							  PRIMARY KEY (`blog_category_id`,`store_id`)
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");
							
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_categories_to_store` (
							  `blog_category_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `store_id` int(10) unsigned NOT NULL DEFAULT '0',
							  PRIMARY KEY (`blog_category_id`,`store_id`)
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_comments` (
							  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
							  `blog_post_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `customer_id` int(10) unsigned NOT NULL DEFAULT '0',						  
							  `author_name` varchar(255) NOT NULL,
							  `author_email` varchar(255) NOT NULL,
							  `author_ip` varchar(255) NOT NULL,
							  `content` text NOT NULL,
							  `blog_comment_id` int(10) unsigned NOT NULL,
							  `approved_by` smallint(5) unsigned NOT NULL DEFAULT '0',
							  `approved` tinyint(1) unsigned NOT NULL DEFAULT '0',
							  `published` tinyint(1) unsigned NOT NULL DEFAULT '0',
							  `created` datetime NOT NULL,
							  `modified` datetime NOT NULL,
							  `rating` int(1) NOT NULL,
							  PRIMARY KEY (`id`),
							  KEY `blog_post_id_published` (`approved`,`blog_post_id`),
							  KEY `blog_post_id` (`blog_post_id`)
							) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
									
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_images` (
						  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
						  `blog_post_id` int(10) unsigned NOT NULL DEFAULT '0',
						  `title` varchar(255) NOT NULL,
						  `image` varchar(255) NOT NULL,
						  `link` varchar(255) NOT NULL,
						  `published` tinyint(1) unsigned NOT NULL DEFAULT '0',
						  `created` datetime NOT NULL,
						  `modified` datetime NOT NULL,
						  `created_by` smallint(5) unsigned NOT NULL DEFAULT '0',
						  `modified_by` smallint(5) unsigned NOT NULL DEFAULT '0',
						  PRIMARY KEY (`id`)
						) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
											
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_posts` (
							  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
							  `comments_count` smallint(5) unsigned NOT NULL DEFAULT '0',
							  `published` tinyint(1) unsigned NOT NULL DEFAULT '0',
							  `created` datetime NOT NULL,
							  `created_by` tinyint(3) unsigned NOT NULL DEFAULT '0',
							  `modified` datetime NOT NULL,
							  `modified_by` tinyint(3) unsigned NOT NULL DEFAULT '0',
							  `sort_order` tinyint(3) NOT NULL,
							  PRIMARY KEY (`id`),
							  KEY `published` (`published`)
							) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
							
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_posts_description` (
							  `blog_post_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `language_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `title` varchar(255) NOT NULL,
							  `description` mediumtext NOT NULL
							) ENGINE=MyISAM DEFAULT CHARSET=latin1");
							
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_posts_to_blog_categories` (
							  `blog_post_id` int(11) NOT NULL,
							  `blog_category_id` int(11) NOT NULL,
							  PRIMARY KEY (`blog_post_id`,`blog_category_id`)
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");
							
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_posts_to_layout` (
							  `blog_post_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `store_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `layout_id` int(10) unsigned NOT NULL DEFAULT '0',
							  PRIMARY KEY (`blog_post_id`,`store_id`)
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");
							
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_posts_to_store` (
							  `blog_post_id` int(10) unsigned NOT NULL DEFAULT '0',
							  `store_id` int(10) unsigned NOT NULL DEFAULT '0',
							  PRIMARY KEY (`blog_post_id`,`store_id`)
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");
							
							
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_links` (
							   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
								`created` datetime NOT NULL,
								`modified` datetime NOT NULL,
								PRIMARY KEY (`id`)
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");
							
							
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "blog_links_description` (
									`blog_link_id` int(10) unsigned NOT NULL DEFAULT '0',
								  `language_id` int(10) unsigned NOT NULL DEFAULT '0',
								  `name` varchar(255) NOT NULL,
								  `href` varchar(255) NOT NULL,
								  `published` tinyint(1) unsigned NOT NULL DEFAULT '0',
								  `sort_order` tinyint(1) unsigned NOT NULL DEFAULT '0',
								  `target` tinyint(1) unsigned NOT NULL DEFAULT '0'
							) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin");

		$this->db->query("INSERT INTO `" . DB_PREFIX . "layout` (`layout_id`, `name`) VALUES	(NULL, 'Blog')");
		$this->db->query("INSERT INTO `" . DB_PREFIX . "layout_route` (`layout_route_id`, `layout_id`, `store_id`, `route`) VALUES (NULL, @@IDENTITY , 0, 'blog/post')");
		
		/* Adding Blog To Menu */
	
		/* Header File Path */
		$header_file_path = DIR_CATALOG.'view/theme/default/template/common/header.tpl';
		/* Header File BackUp Path  */
		$header_file_bk_path = DIR_CATALOG.'view/theme/default/template/common/header.tpl.bk';
		/* Backup header */
		@copy($header_file_path, $header_file_bk_path);
		
		/* Header File Content Fetch */
		$file_content = @file_get_contents($header_file_path);

		
		$this->language->load('blog/setup');

		$blog_url = str_replace(array(HTTP_SERVER,HTTP_CATALOG,HTTPS_SERVER,HTTPS_CATALOG), '', $this->url->link('blog/post'));
		
		/* Add Blog Menu To Header */
		$file_content_replaced = str_replace(
			'<a href="<?php echo $home; ?>"><?php echo $text_home; ?></a>',
			'<a href="<?php echo $home; ?>"><?php echo $text_home; ?></a><a href="'.$blog_url.'">'.$this->language->get('menu_label').'</a>',
			$file_content
		);
	
		/* Updated Header If Blog Menu Added */
		if(strlen($file_content) != strlen($file_content_replaced)){
			@file_put_contents($header_file_path, $file_content_replaced);
		}
		
		
		
		
		/* 
		* We are using this easy an automatic activation request process 
		* We will not sale your contact info, we are collecting your 
		* details just to serve you better in future updated of this extension
		* also contact me at saif@webberoo.com.au for support queries.
		*/
		
		
		$store_name  		= $this->config->get('config_name');  				
		$store_owner 		= $this->config->get('config_owner');  				/* Store Owner */
		$store_address  	= $this->config->get('config_address');  			/* Store Address */
		$store_email		= $this->config->get('config_email');  				/* Store Email */
		$store_tel 			= $this->config->get('config_telephone');  			/* Store Telephone */
		$store_fax 			= $this->config->get('config_fax');  				/* Store Fax */
		
		$store_country  	= $this->config->get('config_country_id');  		/* Country */
		$store_region 		= $this->config->get('config_zone_id');  			/* Region */
		$store_lang 		= $this->config->get('config_language');  			/* Store Language */
		$store_admin_lang 	= $this->config->get('config_admin_language');  	/* Store Admin Language */
			
		$subject = 'Opencart Webby Blog V1.1 Free Activation Request '.$store_name.'.';
		
		$message  = 'Store Name				: '.$store_name. "\n\n";
		$message .= 'Store Owner			: '.$store_owner. "\n\n";
		$message .= 'Store Address			: '.$store_address. "\n\n";
		$message .= 'Store Email			: '.$store_email. "\n\n";
		$message .= 'Store Telephone		: '.$store_tel. "\n\n";
		$message .= 'Store Fax				: '.$store_fax. "\n\n";
		$message .= 'Store Country			: '.$store_country. "\n\n";
		$message .= 'Store Region			: '.$store_region. "\n\n";
		$message .= 'Store Language			: '.$store_lang. "\n\n";
		$message .= 'Store Admin Language	: '.$store_admin_lang. "\n\n";
		
		$mail = new Mail();
		$mail->protocol = $this->config->get('config_mail_protocol');
		$mail->parameter = $this->config->get('config_mail_parameter');
		$mail->hostname = $this->config->get('config_smtp_host');
		$mail->username = $this->config->get('config_smtp_username');
		$mail->password = $this->config->get('config_smtp_password');
		$mail->port = $this->config->get('config_smtp_port');
		$mail->timeout = $this->config->get('config_smtp_timeout');				
		$mail->setTo('saif@webberoo.com.au');
		$mail->setFrom($this->config->get('config_email'));
		$mail->setSender($this->config->get('config_name'));
		$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
		$mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
		$mail->send();
		
	}
	
	public function deleteTables() {
		$this->db->query("DROP TABLE 
		`" . DB_PREFIX . "blog_categories`, 
		`" . DB_PREFIX . "blog_categories_description`, 
		`" . DB_PREFIX . "blog_categories_to_layout`, 
		`" . DB_PREFIX . "blog_categories_to_store`, 
		`" . DB_PREFIX . "blog_comments`, 
		`" . DB_PREFIX . "blog_images`, 
		`" . DB_PREFIX . "blog_posts`, 
		`" . DB_PREFIX . "blog_posts_description`, 
		`" . DB_PREFIX . "blog_posts_to_blog_categories`, 
		`" . DB_PREFIX . "blog_posts_to_layout`, 
		`" . DB_PREFIX . "blog_posts_to_store`,
		`" . DB_PREFIX . "blog_links`, 
		`" . DB_PREFIX . "blog_links_description`");
		
		$result = $this->db->query("SELECT `layout_id` FROM `" . DB_PREFIX . "layout` WHERE `name`='Blog'");
		if($result->num_rows>0){
			$this->db->query("DELETE FROM `" . DB_PREFIX . "layout` WHERE `layout_id`='".$result->row['layout_id']."'");
			$this->db->query("DELETE FROM `" . DB_PREFIX . "layout_route` WHERE `layout_id`='".$result->row['layout_id']."'");
		}
		
		/* Header File Path */
		$header_file_path = DIR_CATALOG.'view/theme/default/template/common/header.tpl';
		/* Header File BackUp Path  */
		$header_file_bk_path = DIR_CATALOG.'view/theme/default/template/common/header.tpl.bk';
		/* Backup header */
		@copy($header_file_path, $header_file_bk_path);
		
		/* Header File Content Fetch */
		$file_content = @file_get_contents($header_file_path);

		$this->language->load('blog/setup');
		/* Add Blog Menu To Header */
		$file_content_replaced = str_replace(
			'<a href="'.$this->url->link('blog/post').'">'.$this->language->get('menu_label').'</a>',
			'',
			$file_content
		);
	
		/* Updated Header If Blog Menu Added */
		if(strlen($file_content) != strlen($file_content_replaced)){
			@file_put_contents($header_file_path, $file_content_replaced);
		}
		
		
		/* 
		* We are using this easy an automatic activation request process 
		* We will not sale your contact info, we are collecting your 
		* details just to serve you better in future updated of this extension
		* also contact me at saif@webberoo.com.au for support queries.
		*/
		
		
		$store_name  		= $this->config->get('config_name');  				
		$store_owner 		= $this->config->get('config_owner');  				/* Store Owner */
		$store_address  	= $this->config->get('config_address');  			/* Store Address */
		$store_email		= $this->config->get('config_email');  				/* Store Email */
		$store_tel 			= $this->config->get('config_telephone');  			/* Store Telephone */
		$store_fax 			= $this->config->get('config_fax');  				/* Store Fax */
		
		$store_country  	= $this->config->get('config_country_id');  		/* Country */
		$store_region 		= $this->config->get('config_zone_id');  			/* Region */
		$store_lang 		= $this->config->get('config_language');  			/* Store Language */
		$store_admin_lang 	= $this->config->get('config_admin_language');  	/* Store Admin Language */
			
		$subject = 'Opencart Webby Blog V1.1 Uninstalled By '.$store_name.'.';
		
		$message  = 'Store Name				: '.$store_name. "\n\n";
		$message .= 'Store Owner			: '.$store_owner. "\n\n";
		$message .= 'Store Address			: '.$store_address. "\n\n";
		$message .= 'Store Email			: '.$store_email. "\n\n";
		$message .= 'Store Telephone		: '.$store_tel. "\n\n";
		$message .= 'Store Fax				: '.$store_fax. "\n\n";
		$message .= 'Store Country			: '.$store_country. "\n\n";
		$message .= 'Store Region			: '.$store_region. "\n\n";
		$message .= 'Store Language			: '.$store_lang. "\n\n";
		$message .= 'Store Admin Language	: '.$store_admin_lang. "\n\n";
		
		$mail = new Mail();
		$mail->protocol = $this->config->get('config_mail_protocol');
		$mail->parameter = $this->config->get('config_mail_parameter');
		$mail->hostname = $this->config->get('config_smtp_host');
		$mail->username = $this->config->get('config_smtp_username');
		$mail->password = $this->config->get('config_smtp_password');
		$mail->port = $this->config->get('config_smtp_port');
		$mail->timeout = $this->config->get('config_smtp_timeout');				
		$mail->setTo('saif@webberoo.com.au');
		$mail->setFrom($this->config->get('config_email'));
		$mail->setSender($this->config->get('config_name'));
		$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
		$mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
		$mail->send();
	}
}
?>